package com.bringway.p2p.controller;

import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.ZqzrService;

import net.sf.json.JSONObject;
/**  
 * @类名 ZqzrController  
 * @功能 TODO  新债权转让后台界面
 * @作者 js 
 * @日期 2016年7月11日 上午11:24:21  
 *    
 */
@Controller
@RequestMapping("/zqzr")
public class ZqzrController extends JsonController {
	
	Logger logger=Logger.getLogger(ZqzrController.class);
	
	@Autowired
	private ZqzrService zqzrService;
	
	/**
	 * @作者 js  
	 * @方法名 queryZqzrsqList  
	 * @功能 TODO 查询债权转让申请列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZqzrsqList")
	public ModelAndView queryZqzrsqList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String czrr=RequestUtils.getStringParameter(request, "czrr");
		String cshr=RequestUtils.getStringParameter(request, "cshr");
		String csqzt=RequestUtils.getStringParameter(request, "csqzt");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("czrr", czrr);
		params.put("cshr", cshr);
		params.put("csqzt", csqzt);
		List<Map<String, Object>> list=zqzrService.queryZqzrsqList(params);
		int count=zqzrService.getZqzrsqCount(params);
		
		result.put("data", list);
		result.put("total", count);
		
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 saveZqzrsq  
	 * @功能 TODO 新增债权申请
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveZqzrsq")
	public ModelAndView saveZqzrsq(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String tzid=RequestUtils.getStringParameter(request, "tzid");
		String zrr=RequestUtils.getStringParameter(request, "zrr");
		String zrje=RequestUtils.getStringParameter(request, "zrje");
		String zrkssj=RequestUtils.getStringParameter(request, "zrkssj");
		String zrjssj=RequestUtils.getStringParameter(request, "zrjssj");
		String zrsxf=RequestUtils.getStringParameter(request, "zrsxf");
		String sqbz=RequestUtils.getStringParameter(request, "sqbz");
		JSONObject params=new JSONObject();
		JSONObject result=new JSONObject();
		params.put("tzid", tzid);
		params.put("zrr", zrr);
		params.put("zrje", zrje);
		params.put("zrkssj", zrkssj);
		params.put("zrjssj", zrjssj);
		params.put("zrsxf", zrsxf);
		params.put("sqbz", sqbz);
		boolean isAddSuc=zqzrService.saveZqzrsq(params);
		result.put("success", true);
		result.put("isAddSuc", isAddSuc);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 saveZqzrsh  
	 * @功能 TODO 审核债权申请
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveZqzrsh")
	public ModelAndView saveZqzrsh(HttpServletRequest request,HttpServletResponse response) throws Exception {
		//SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String sqid=RequestUtils.getStringParameter(request, "sqid");
		String shr=RequestUtils.getStringParameter(request, "shr");
		String shzt=RequestUtils.getStringParameter(request, "shzt");
		String shbz=RequestUtils.getStringParameter(request, "shbz");
		String zrr=RequestUtils.getStringParameter(request, "zrr");
		String zrje=RequestUtils.getStringParameter(request, "zrje");
		String zrsxf=RequestUtils.getStringParameter(request, "zrsxf");
		String zrsqrq=RequestUtils.getStringParameter(request, "zrsqrq");
		String tzid=RequestUtils.getStringParameter(request, "tzid");
		JSONObject params=new JSONObject();
		JSONObject result=new JSONObject();
		params.put("sqid", sqid);
		params.put("shr", shr);
		params.put("shzt", shzt);
		params.put("shbz", shbz);
		params.put("zrr", zrr);
		params.put("zrje", zrje);
		params.put("zrsxf", zrsxf);
		params.put("zrsqrq", zrsqrq);
		params.put("tzid", tzid);
		Map<String, Object> zqzrsqMap = zqzrService.getZqzrsqInfo(params);
		if(null == zqzrsqMap){
			result.put("isSHSuc", false);
			result.put("msg", "信息有误！");
		}else if(zqzrsqMap.containsKey("ZR_SQ_STATE")){
			Object stateObj = zqzrsqMap.get("ZR_SQ_STATE");
			if(null != stateObj && stateObj.toString().equals("01")){//待审核状态才可以审核
				try{
					boolean isSHSuc = zqzrService.saveZqzrsh(params);
					result.put("isSHSuc", isSHSuc);
				}catch (Exception e) {
					e.printStackTrace();
					result.put("isSHSuc", false);
					result.put("msg", "审核失败！");
				}
			}else{//无审核状态信息,无法审核
				result.put("isSHSuc", false);
				result.put("msg", "该申请已审核过，请勿重复操作！");
			}
		}
		result.put("success", true);
		serialize(request,response,result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryZqzrlbList  
	 * @功能 TODO 查询审核通过的列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZqzrlbList")
	public ModelAndView queryZqzrlbList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String zrr=RequestUtils.getStringParameter(request, "zrr");
		String srr=RequestUtils.getStringParameter(request, "srr");
		String htbh=RequestUtils.getStringParameter(request, "htbh");
		String zrzt=RequestUtils.getStringParameter(request, "zrzt");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("zrr", zrr);
		params.put("htbh", htbh);
		params.put("srr", srr);
		params.put("zrzt", zrzt);
		List<Map<String, Object>> list=zqzrService.queryZqzrlbList(params);
		int count=zqzrService.getZqzrlbCount(params);
		float sumJe=zqzrService.getZqzrlbSumje(params);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJe);
		serialize(request, response, result);
		return null;
	}
	
}
